<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class QdService extends BaseService
{
    //渠道

    /**
     * 分类列表
     */
    public function getClist()
    {
        $db = $this->dbwbConn($this->dbQdCate)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有分类');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $pOne = (array) $this->dbwbConn($this->dbQdCate)->where('id', $v['pid'])->first();
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $this->imgDomain.'/'.$v['img'],
                'pname' => empty($pOne) ? '一级分类' : $pOne['title'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 商品列表
     */
    public function getGlist()
    {
        $db = $this->dbwbConn($this->dbQdGoods)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有商品');
        }
        $arys = [];
        foreach ($res as $v ) {
            $v = (array) $v;
            $cateOne = (array) $this->dbwbConn($this->dbQdCate)
                ->where('del', 0)
                ->where('id', $v['cate_id'])
                ->first();
            $cate_name = !empty($cateOne) ? $cateOne['title'] : '';
            $arys[] = [
                'id' => $v['id'],
                'gno' => $v['gno'],
                'title' => $v['title'],
                'img' =>  $this->imgDomain.'/'.$v['bigimg'],
                'cate_name' => $cate_name,
                'sale' => $v['sale'],
                'isnew' => $v['isnew'],
                'is_in_reason' => $v['is_in_reason'],
                'price_one' => toNumber($v['price_one']),
                'price_two' => toNumber($v['price_two']),
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 订单列表
     */
    public function getOlist()
    {
        $db = $this->dbwbConn($this->dbQdOrder)->where('del', 0);
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有分类');
        }
        $statuss = $this->qdOrderStatuss;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $skuOnes = $this->dbwbConn($this->dbQdOsku)->where('order_id', $v['id'])->get();
            $ginfo = '';
            if (count($skuOnes)) {
                foreach ($skuOnes as $vv) {
                    $vv = (array) $vv;
                    $ginfo .= '商品名称：'.$vv['gname'];
                    $ginfo .= 'Sku名称：'.$vv['sku_name'];
                    $ginfo .= '商品单价：'.$vv['price']/100;
                    $ginfo .= '商品数量：'.$vv['num'];
                    $ginfo .= '；';
                }
            }
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '';
            $acceptOne = (array) $this->dbwbConn($this->dbQdAddress)
                ->where('uid', $v['uid'])
                ->where('selected', 1)
                ->first();
            $accepter = empty($acceptOne) ? '-' : $acceptOne['accepter'];
            $accept_address = empty($acceptOne) ? '-' : $acceptOne['wx_door'];
            $arys[] = [
                'id' => $v['id'],
                'orderno' => $v['orderno'],
                'uname' => $v['uname'],
                'ginfo' => $ginfo,
                'price' => toNumber($v['price']),
                'accepter' => $accepter,
                'accept_address' => $accept_address,
                'status_name' => $status_name,
            ];
        }
        return $this->toback(0, '', $arys);
    }
}